<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <h1>文件上传</h1>
        <input type="file" ref="fl" @change="fn" accept=".jpg,.jpeg,.png,.pdf">
        <button>点击文件上传</button>
    </div>

</body>

</html>
<script src="./vue2.js"></script>
<script src="./axios.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                file: ''
            }
        },
        methods: {
            fn() {
                const file = this.$refs.fl.files[0]
                console.log(file);
                if (!file) return;
                if (file.size > 1024 * 1000) return alert("限制1000Kb内图片上传")


                let formData = new FormData(); // 创建FormData对象
                formData.append('file', file); // 添加文件到formData对象
                axios({
                    method: 'post',
                    url: 'http://127.0.0.1:3006/shop/upload', // 设置上传接口地址
                    data: formData,
                    headers: { 'Content-Type': 'multipart/form-data' } // 设置请求头为multipart/form-data类型
                }).then(res => {
                    console.log(res);
                })

            }
        }
    })
</script>